import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入多个模块的规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'
Vue.use(VueRouter)

/* Layout */
import Layout from '@/layout'

// 假设后端返给我们的有权限的路由名字是
// const menuRouter = ['settings', 'social_securitys', 'permissions']
// 静态路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]
// 动态路由
export const asyncRoutes = [
  approvalsRouter,
  departmentsRouter,
  employeesRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  settingRouter,
  socialRouter
]

// const newAsyncRoutes = []
// // 通过筛选的方法, 生成一个新的动态路由, 再解构路由配置中
// asyncRoutes.forEach(obj => {
//   // ②在menu数组内查, obj.name
//   // 1   obj => {路由配置对象}
//   const flag = menuRouter.includes(obj.name)
//   if (flag) {
//     newAsyncRoutes.push(obj)
//   }
// })

const createRouter = () => new VueRouter({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: [...constantRoutes, ...asyncRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

// router.beforeEach((to, from, next) => {})

export default router

// const obj = [{ name: 'zs' }, { name: 'ls' }, { name: 'ww' }]
// const menu = ['zs', 'ww']
// // 筛选name值和menu匹配的, 形成一个新数组
// // ①循环obj
// const newArr = []
// obj.forEach(obj => {
//   // ②在menu数组内查, obj.name
//   // 1   obj => {name: 'zs}     menu.includes('zs') => true
//   const flag = menu.includes(obj.name)
//   if(flag) {
//     newArr.push(obj)
//   }
// })
